package org.pictual.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.util.Enumeration;

import javax.swing.text.MutableAttributeSet;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.parser.ParserDelegator;

import org.apache.log4j.Logger;
import org.pictual.controller.DescargaImagen;

import com.infinityk.simplesoundengine.Log;

public class GetLinks extends HTMLEditorKit.ParserCallback{

	public static String oggLink;
	protected static Logger log= Logger.getLogger(GetLinks.class);
	
	public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos) {
		if (t == HTML.Tag.A) {
			String link = null;
			Enumeration<?> attributeNames = a.getAttributeNames();
			if (attributeNames.nextElement().equals(HTML.Attribute.HREF)){
				link = a.getAttribute(HTML.Attribute.HREF).toString();
				if( (link.contains("upload.wikimedia.org/wikipedia/commons"))&&(link.contains(".ogg")) )
					oggLink="http:"+link;
			}
		}
	}

	public static void main(String[] args) {
		try {
			URL url = new URL("http://commons.wikimedia.org/wiki/File:Buenas_noches.ogg");

			Reader reader= new InputStreamReader((InputStream) url.getContent());

			new ParserDelegator().parse(reader, new GetLinks(), true);
			
			log.info(GetLinks.oggLink);
		} catch (IOException e) {
			log.error("IOException al descargar enlaces de wiki para el audio");
		}
	}
}
